﻿using DrCarDrive.DBService;
using DrCarDrive.Util;
using DrCarDrive.Wpf.WpfWindow;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for RecordHistory.xaml
    /// </summary>
    public partial class RecordHistoryPage : Page
    {
        private static RecordHistoryPage instance;
        public RecordHistoryPage()
        {
            InitializeComponent();
            //List<tbl_drive> drives = new List<tbl_drive>();
            //drives.Add(new tbl_drive() { START_DATE = new DateTime(2005,12,1), END_DATE = new DateTime(2005,12,25), START_ODOMETER = 100, FROM = "Kandy", TO = "Kadugannawa", END_ODOMETER = 200 });
            //drives.Add(new tbl_drive() { START_DATE = new DateTime(2006, 12, 1), END_DATE = new DateTime(2006, 12, 25), START_ODOMETER = 200, FROM = "Katugastota", TO = "Kadugannawa", END_ODOMETER = 300 });

           
        }

        public static RecordHistoryPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new RecordHistoryPage();
                }
                return instance;
            }
        }


        public void viewHistoryByCarID()
        {
           DriveService service = new DriveService();
           List<DriveHistoryView> driveHistoryViewList = new List<DriveHistoryView>();
           driveHistoryViewList = service.viewHistoryRecordByCarID(SessionData.car.ID);
           MessageBox.Show(driveHistoryViewList[0].DriveID);
       
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
           // viewHistoryByCarID();
            
        }

        private void Page_Loaded_2(object sender, RoutedEventArgs e)
        {
            if (SessionData.drive != null)
            {
                historyViewLoadDate(SessionData.drive);
            }
        }

        private void historyViewLoadDate(tbl_drive drive)
        {
            historyViewReset();
            
            if (drive != null)
            {
                lblStartDateTime.Content = drive.START_DATE;
                lblStartOdometer.Content = drive.START_ODOMETER;
                lblStartFrom.Content = drive.FROM;

                if (drive.STATUS == true)
                {
                    lblEndDateTime.Content = "On Drive";
                }
                else
                {
                    lblEndDateTime.Content = drive.END_DATE;
                }
                lblEndOdometer.Content = drive.END_ODOMETER;
                lblEndTo.Content = drive.TO;

                lblDriverName.Content = drive.DRIVER_NAME;
                lblDuration.Content = FindDifference.calculateDuration(drive.START_DATE, drive.END_DATE);
                lblDistance.Content = FindDifference.calculateDistance(drive.START_ODOMETER, drive.END_ODOMETER);

                DriveService service = new DriveService();
                List<DriveHistoryView> driveHistoryViewList = service.viewHistoryRecordByDriveID(drive.ID);
                //tbl_places pls = driveHistoryViewList[0].Places;
                ExpensesService expensesService = new ExpensesService();
                List<ExpensesView> expenses = expensesService.viewByDriveID(drive.ID);

                FuelTopupService fuelService = new FuelTopupService();
                List<tbl_fuel> fuelTopup = fuelService.viewByDriveID(drive.ID);

                gridHealthCenterHistoryRecords.ItemsSource = driveHistoryViewList[0].Places;
                gridExpensesHistoryRecords.ItemsSource = expenses;
                gridFuelTopUpHistoryRecords.ItemsSource = fuelTopup;
            }
        }

        private void historyViewReset()
        {
            lblStartDateTime.Content = "";
            lblStartOdometer.Content = "";
            lblStartFrom.Content = "";

            lblEndDateTime.Content = "";
            lblEndOdometer.Content = "";
            lblEndTo.Content = "";

            lblDriverName.Content = "";
            lblDuration.Content = "";
            lblDistance.Content = "";

            gridHealthCenterHistoryRecords.ItemsSource = null;
            gridExpensesHistoryRecords.ItemsSource = null;
            gridFuelTopUpHistoryRecords.ItemsSource = null;
        }

        private void btnDetails_Click(object sender, RoutedEventArgs e)
        {
            MedicineService medicineService = new MedicineService();
            string placeID = ((Button)sender).CommandParameter.ToString();
            List<IssueMedicineView> medicine = medicineService.getMedicineByPlaceID(placeID);

            PlaceService placeService = new PlaceService();
            tbl_places place = placeService.viewByPlaceID(placeID);
            MainWindow.Instance.gridIssuedMedicine.ItemsSource = medicine;
            MainWindow.Instance.setFlyOutValues(place, medicine);

            MainWindow.Instance.flyHistoryDetails.IsOpen = true;
        }

        private void gridHealthCenterHistoryRecords_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dpDriveDate_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            
        }

        private void dpDriveDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            DriveService driveService = new DriveService();
            tbl_drive drive = driveService.viewByCarIDStartDate(SessionData.car.ID, dpDriveDate.SelectedDate);
            historyViewLoadDate(drive);
        }
       
    }
}
